//This is set for the orientation towards the Z

private var hitParticles : ParticleEmitter; //spark particle for when we hit something
private var spreadX : float = 0.0;
private var spreadY : float = 0.0;
private var currentSpread : float = 0.0;
var spreadMax : float = .1; //spread reange. 
var range = 100.0; // range of the Raycast basically the range of the bullet as well
var bulletsPerShot= 1; //public var on the Inspector. How much bullets we want to shoot on one click.

function Start(){
	
	hitParticles = GetComponentInChildren(ParticleEmitter); //find the spark particle under Gun and set it
	if(hitParticles){
		hitParticles.emit = false; //stop emitting until we hit something
	}

}
function Update () {

    if (Input.GetMouseButtonDown(0)) { //wait for mouse1
		Debug.Log("Fired"); 
		fireShot();//start firing
	}
}

function fireShot(){
	var direction; //the direction of the ray
	var hit : RaycastHit; // container for information about what we hit
	var otherScript; //place to store the die script
	var otherObj : GameObject; //the object we hit
	for (i = 0; i < bulletsPerShot - 1; i++){ //loop for however many shots we shoot - 1 as one shot is always accurate
		calcSpread(); //find a random spread
		Debug.Log("Spread is" + currentSpread);
		//First find the forward vector then add the random spread vectors to it.
		direction =  transform.TransformDirection(Vector3.forward) + transform.TransformDirection(randomX(), randomY(), 0); 
		Debug.Log("Y is " + spreadY + " X is " + spreadX);
		if (Physics.Raycast (transform.position, direction, hit, range)) { //cast our ray out using the transforms current position as a starting point. Raycast will return true or false based on a collision
				otherObj = hit.collider.gameObject; //set what we hit
				//Debug.Log("Shot at: " + otherObj.tag);
				if (otherObj.tag == "Skeet"){ //if we hit a Skeet we need to kill it
					otherScript = otherObj.GetComponent(Die);
					otherScript.Die();
				}
				if (hitParticles) { //create a spark at anything we hit
				hitParticles.transform.position = hit.point;
				Debug.Log("Hit Location" + hit.point);
				hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
				hitParticles.Emit();
				}
		}
	}
	direction =  transform.TransformDirection(Vector3.forward); //find forward again for our accurate shot
	if (Physics.Raycast (transform.position, direction, hit, range)) {
				otherObj= hit.collider.gameObject;
				//Debug.Log("Shot at: " + otherObj.tag);
				if (otherObj.tag == "Skeet"){
					otherScript = otherObj.GetComponent(Die);
					otherScript.Die();
				}
				if (hitParticles) {
				hitParticles.transform.position = hit.point;
				Debug.Log("Hit Location" + hit.point);
				hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
				hitParticles.Emit();
				}
		}
}

function calcSpread(){
	
	currentSpread = Random.Range(0.0, spreadMax); //find a random spread
	currentSpread = Mathf.Clamp (currentSpread, 0.0, spreadMax); //making sure its within the boundry

}

function randomX(){
	spreadX = Random.Range(-currentSpread, currentSpread); //find a random spread within the spread
	return spreadX;
}

function randomY(){
	spreadY = Random.Range(-currentSpread, currentSpread);
	return spreadY;
}